package com.baidu.process.record.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baidu.process.chat.core.web.bean.IMRequestServer;
import com.baidu.process.chat.core.web.bean.IMResponseResult;
import com.baidu.process.imchat.entity.ImchatRequest;
import com.baidu.process.record.bean.DialogueRecord;
import com.baidu.process.record.mapper.RecordMapper;
import com.baidu.process.record.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author:fangyuchao
 * @Description:
 * @Date: Create in 18:07 2022/12/13
 */
@Service
public class RecordServiceImpl implements RecordService {
    @Resource
    private RecordMapper recordMapper;


    @Override
    public List<DialogueRecord> selectAll() {

        return recordMapper.selectList(null);
    }

    /**
     * 异步入库
     * @param
     */
    @Async("processSqlExecutorPool")
    @Override
    public void saveAsyncRecord(DialogueRecord dialogueRecord, IMRequestServer imRequestServer, IMResponseResult imResponseResult,String ngdResJson) {
        //有ngd返回的数据
        if (!StringUtils.isEmpty(ngdResJson)){
            Object dataObj = JSONObject.parseObject(ngdResJson).get("data");
            JSONObject dataJsonObj = JSONObject.parseObject(JSONObject.toJSONString(dataObj));
            String queryId = StringUtils.isEmpty(dataJsonObj.get("queryId"))?"":dataJsonObj.get("queryId").toString();
            String source = StringUtils.isEmpty(dataJsonObj.get("source"))?"":dataJsonObj.get("source").toString();
            String answerTime = StringUtils.isEmpty(dataJsonObj.get("answerTime"))?"":dataJsonObj.get("answerTime").toString();
            dialogueRecord.setQueryId(queryId);
            dialogueRecord.setAnswerTime(answerTime);
            dialogueRecord.setSource(source);
        }
        //有接口返回给调用方的数据
        if (!StringUtils.isEmpty(imResponseResult)){
            String suggestAnswerJson =imResponseResult.getJsonString();
            JSONObject suggestAnswerObj = JSONObject.parseObject(suggestAnswerJson);
            dialogueRecord.setNgdAnswer(StringUtils.isEmpty(suggestAnswerObj.get("answer"))?"":suggestAnswerObj.get("answer").toString());
            dialogueRecord.setBot(StringUtils.isEmpty(suggestAnswerObj.get("bot"))?"":suggestAnswerObj.get("bot").toString());
        }
        dialogueRecord.setChannel(imRequestServer.getChannel());
        dialogueRecord.setQueryText(imRequestServer.getQueryText());
        dialogueRecord.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        dialogueRecord.setSessionId(imRequestServer.getSessionId());
        dialogueRecord.setNgdResponseData(ngdResJson);
        dialogueRecord.setRequestData(JSONObject.toJSONString(imRequestServer));
        dialogueRecord.setToAccount(imRequestServer.getToAccount());
        dialogueRecord.setAskTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(Long.parseLong(imRequestServer.getStartTime()))));
        recordMapper.insert(dialogueRecord);

    }
}